const Router = require('koa-router')
const authMiddleware = require('../middleware/auth')
const checkNoAuth = require('../lib/utils/check-no-auth') // 鉴权白名单
const path = require('path')

const router = new Router({ prefix: '/api/v1' }).use(async (ctx, next) => {
  // await next()
  if (checkNoAuth(ctx.request.path)) {
    await next()
    return
  }
  await authMiddleware()(ctx, next)
})

// 引入 ./modules 下所有 js 文件
const modules = require('glob').sync(path.join(__dirname, './modules/*.js'))
modules.forEach(function (module) {
  const routes = require(module)
  router.use(routes.routes())
})


module.exports = router
